package PHANVANVUONG.SERLALIZABLE.DEMO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Models<T> {
	@SuppressWarnings("resource")
	public boolean WriteListObjIntoFile(List<T> lisObj, String resource, String fileName) throws Exception{
		try{
			String path=resource+"/"+fileName;
			File file=new File(path);
			
			FileOutputStream objOutFile = new FileOutputStream(file);
            ObjectOutputStream objOoS = new ObjectOutputStream(objOutFile);
            objOoS.writeObject(lisObj);
            objOoS.flush();
		}catch(Exception e){
			throw e;
		}
		
		return true;
	}
	
	@SuppressWarnings({ "resource", "unchecked" })
	public List<T> ReadListObjIntoFile(String resource, String fileName) throws Exception{
		String path = resource + "/" + fileName;
        File objFile = new File(path);
        List<T> lisObj=null;
		try{
			
            ObjectInputStream objObjectIn = new ObjectInputStream(new FileInputStream(objFile));
            lisObj = new ArrayList<T>();
            lisObj = (List<T>) objObjectIn.readObject();
		}catch(Exception e){
			throw e;
		}
		return lisObj;
	}
}
